package com.zankong.tool.zkappforas.div_item;

import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;

import com.zankong.tool.zkapp.document.ZKDocument;
import com.zankong.tool.zkapp.item.HolderInit;
import com.zankong.tool.zkapp.item.ZKAdapter;
import com.zankong.tool.zkapp.util.ZKAppAdapter;
import com.zankong.tool.zkappforas.R;

import org.dom4j.Attribute;
import org.dom4j.Element;

import java.util.HashMap;

/**
 * Created by YF on 2018/9/17.
 */

@ZKAppAdapter("task_receiver_list")
public class Task_ReceiverList extends ZKAdapter {
    private HashMap<String,String> mClickMap;
    public Task_ReceiverList(ZKDocument zKDocument, Element element) {
        super(zKDocument, element);
        mClickMap = new HashMap<>();
        for (Attribute attribute : element.element("task_receiver_list").attributes()) {
            mClickMap.put(attribute.getName(),attribute.getValue());
        }
    }

    @Override
    protected RecyclerView.ViewHolder onCreate(@NonNull ViewGroup viewGroup, int i) {
        return new ReceiverListHolder(mLayoutInflater.inflate(R.layout.item_task_receiver_list,viewGroup,false));
    }

    @Override
    protected int getVIewType(int position) {
        return 0;
    }

    @Override
    protected void onBind(@NonNull RecyclerView.ViewHolder viewHolder, int position) {
        if (viewHolder instanceof HolderInit){
            ((HolderInit) viewHolder).init(position);
        }
    }

    private class ReceiverListHolder extends RecyclerView.ViewHolder implements HolderInit {
        ImageView header;
        Button agree;
        public ReceiverListHolder(@NonNull View itemView) {
            super(itemView);
            header = itemView.findViewById(R.id.header);
            agree = itemView.findViewById(R.id.agree);
        }

        @Override
        public void init(int position) {
            header.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mClickMap.containsKey("header")) {
                        invokeWithContext(mClickMap.get("header"),ReceiverListHolder.this);
                    }
                }
            });
            agree.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    if (mClickMap.containsKey("agree")) {
                        invokeWithContext(mClickMap.get("agree"),ReceiverListHolder.this);
                    }
                }
            });
        }
    }
}
